package views;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.ArrayList;
import java.awt.print.PageFormat;

import javax.swing.JFrame;

import net.miginfocom.swing.MigLayout;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import controllers.BagageAfhandelController;
import core.logic.services.utilServices.ValidatieService;
import core.models.dataModels.CheckinOpvragenModel;


public class BagageAfhandelingView extends JFrame
{	
	private static final long serialVersionUID = 1L;
	private JTextField txtNaam;
	private JTextField txtVoornaam;
	private JTextField txtVluchtnr;	
	private JButton btnControle;
	private JLabel lblHandbagage;
	private JButton btnPrintLabel;
	private JButton btnCancel;
	private JButton btnRecheck;
	
	public BagageAfhandelingView(final Integer aantal, final CheckinOpvragenModel model)
	{
		
		JLabel lblNaam = new JLabel("Naam");
		lblNaam.setName("1");
		txtNaam = new JTextField();
		txtNaam.setColumns(10);
		txtNaam.setText(model.getPassagier().getPersoon().getNaam());
		txtNaam.setName("2");
		
		JLabel lblVoornaam = new JLabel("Voornaam");
		lblVoornaam.setName("3");
		txtVoornaam = new JTextField();
		txtVoornaam.setName("6");
		txtVoornaam.setColumns(10);
		txtVoornaam.setText(model.getPassagier().getPersoon().getVoornaam());
		JLabel lblVluchtnr = new JLabel("Vluchtnummer:");
		lblVluchtnr.setName("4");
		txtVluchtnr = new JTextField();
		txtVluchtnr.setName("5");
		txtVluchtnr.setColumns(10);
		txtVluchtnr.setText(model.getPassagier().getVlucht().getVluchtNr());
		
		getContentPane().setLayout(new MigLayout("", "[100px][150px][][][]", "[20px][20px][20px][]"));
		getContentPane().add(lblNaam, "cell 0 0,alignx right,aligny center");
		getContentPane().add(txtNaam, "cell 1 0,growx,aligny top");
		
		btnControle = new JButton("Controleer");
		getContentPane().add(btnControle, "cell 2 0");
		
		btnCancel = new JButton("Annuleer");
		getContentPane().add(btnCancel, "cell 3 0,growx,aligny bottom");
		
		
		btnPrintLabel = new JButton("Print labels");
		btnPrintLabel.setVisible(false);
		getContentPane().add(btnPrintLabel, "cell 4 0");
		getContentPane().add(lblVoornaam, "cell 0 1,alignx right,aligny center");
		getContentPane().add(txtVoornaam, "cell 1 1,growx,aligny top");
		
		btnRecheck = new JButton("Opnieuw controleren");
		getContentPane().add(btnRecheck, "cell 3 1");
		getContentPane().add(lblVluchtnr, "cell 0 2,alignx right,aligny center");
		getContentPane().add(txtVluchtnr, "cell 1 2,growx,aligny top");
		
		lblHandbagage = new JLabel("Handbagage?");
		getContentPane().add(lblHandbagage, "cell 2 2");
		
		addLabels(aantal);		
		final ArrayList<JTextField>listTextFieldBagage = addTextFields(aantal);		
		final ArrayList<JCheckBox>listCheckBoxBagage = addCheckBox(aantal);
		
		btnCancel.addActionListener(new ActionListener()
		{			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				if(e.getSource()==btnCancel)
				{
					BagageAfhandelController.handleView();
				}
				
			}
		});		
		btnControle.addActionListener(new ActionListener()
		{			
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				if(arg0.getSource()==btnControle)
				{
					if(validatieOk(aantal,listTextFieldBagage))
					{
						validateOK(listTextFieldBagage, listCheckBoxBagage);
					}
				}
			}
			
		});
		btnRecheck.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				if(arg0.getSource()==btnRecheck)
				{
					btnPrintLabel.setVisible(false);
					validateOK(listTextFieldBagage, listCheckBoxBagage);
				}
				
			}
		});
		btnPrintLabel.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				if(e.getSource()==btnPrintLabel)
				{
					ArrayList<Component> list = new ArrayList<Component>();
					for(Component c: BagageAfhandelingView.this.getContentPane().getComponents())
					{						
						@SuppressWarnings("unused")
						int i = 0;
						try
						{
							i = Integer.parseInt(c.getName());
						}
						catch(NumberFormatException nex)
						{
							list.add(c);
							c.setVisible(false);
						}
					}					
					print(BagageAfhandelingView.this);					
					for(Component c: list)
					{
						c.setVisible(true);
					}
					
				}
				
			}
		});
		
	}
			private void validateOK(final ArrayList<JTextField> listTextFieldBagage, final ArrayList<JCheckBox> listCheckBoxBagage)
			{
				int i=0;
				int h=0;
				boolean ok = true;
				for(JCheckBox j:listCheckBoxBagage)
				{							
					if(!j.isSelected())
					{
						JTextField text = listTextFieldBagage.get(i);
						int gewicht = Integer.parseInt(text.getText());
						if(gewicht > 20)
						{
							//Unchecked
							JOptionPane.showMessageDialog(null, "Bagage te zwaar");
							ok = false;									
						}
					}
					else if(j.isSelected())
					{
						//Checked
						h++;
						if(h>1)
						{
							JOptionPane.showMessageDialog(null, "Er mag slechts 1 item als handbagage meegenomen worden per persoon!");
							ok = false;
						}
						
					}
					i += 1;							
				}
				if(ok == true)
				{
					btnPrintLabel.setVisible(true);
					btnControle.setVisible(false);
				}
			};
	
		
			public void print(JFrame t)
			{
				final JFrame frame = t;
				PrinterJob job = PrinterJob.getPrinterJob();
				job.setJobName("Print component");
				
				job.setPrintable(new Printable()		
				{
					
					@Override
					public int print(Graphics arg0, PageFormat arg1, int arg2)
							throws PrinterException
					{
						if(arg2>0)
						{
							return Printable.NO_SUCH_PAGE;
						}
						Graphics2D g2d = (Graphics2D)arg0;
						g2d.translate(arg1.getImageableX(), arg1.getImageableY());
						g2d.scale(0.50, 0.5);
						frame.getContentPane().paint(g2d);
						return Printable.PAGE_EXISTS;
					}
				});
				
				if(job.printDialog()== false)
				{
					JOptionPane.showMessageDialog(null, "Printen geannuleerd");
					return;
				}
				
				try
				{
					job.print();
					JOptionPane.showMessageDialog(null, "Printen is klaar");
				}
				
				catch (PrinterException ex)
				{
					ex.getMessage();
				}
			}
	
	private boolean validatieOk(int a, ArrayList<JTextField> list)
	{
		if(!ValidatieService.isPresentAndLength(txtNaam, 150	, "Naam"))
		{
			return false;
		}
		if(!ValidatieService.isPresentAndLength(txtVoornaam, 150, "Voornaam"))
		{
			return false;
		}
		if(!ValidatieService.isPresentAndLength(txtVluchtnr, 45	, "Vluchtnummer"))
		{
			return false;
		}
		for(JTextField t: list)
		{
			if(!ValidatieService.isDouble(t, 2, t.getName()))
			{
				return false;
			}
		}
		return true;
	}
	private ArrayList<JCheckBox> addCheckBox(int a)
	{
		ArrayList<JCheckBox> list =new ArrayList<JCheckBox>();
		String naam = "cbBagage ";
		int rij = 4;
		int kolom = 2;
		String cell = "cell " + kolom + " " + rij ;
		for(int i = 1; i<=a; i++)
		{
			naam = naam.substring(0,9);
			naam += i;
			
			JCheckBox test = new JCheckBox();
			test.setName(naam);
			getContentPane().add(test,cell);
			rij++;
			cell = "cell " + kolom + " " + rij;
			list.add(test);
		}
		return list;
	}
	private ArrayList<JTextField> addTextFields(int a)
	{
		ArrayList<JTextField> list = new ArrayList<JTextField>();
		String naam = "txtBagage ";
		int rij = 4;
		int kolom = 1;
		String cell = "cell " + kolom + " " + rij + ", growx"; 
		for(int i=1; i<= a;i++)
		{
			naam = naam.substring(0, 10);
			naam += i;
			
			JTextField test = new JTextField();
			test.setName(naam);			
			getContentPane().add(test,cell);
			rij++;
			cell= "cell " + kolom + " " + rij + ", growx" ;
			list.add(test);
		}
		return list;
	}
	
	private void addLabels(int a)
	{
		String naam = "Gewicht bagage ";
		int rij = 4;
		int kolom = 0;
		String cell = "cell " + kolom + " " + rij + ", growx";
		for(int i=1; i<=a; i++)
		{
			naam = naam.substring(0, 15);
			naam += i + ":";
			
			JLabel test = new JLabel(naam);
			test.setName(naam);
			getContentPane().add(test, cell);
			rij++;
			cell = "cell " + kolom + " " + rij + ", growx";
		}
	}	
}
